package com.hc.controller;

import com.hc.bean.PageBean;
import com.hc.bean.QueryBean;
import com.hc.domain.Province;
import com.hc.domain.query.ProvinceQuery;
import com.hc.ex.WeGoArgumentException;
import com.hc.service.ProvinceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "省接口")
@Controller
@RequestMapping("/province")
public class ProvinceController {
    @Resource
    private ProvinceService provinceService;

    @ApiOperation(value = "省份列表")
    @GetMapping("/list")
    public ModelAndView list(QueryBean queryBean,
                             @RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
                             @RequestParam(defaultValue = "10", name = "pageSize") Integer pageSize,
                             ModelAndView mav) {
        ProvinceQuery provinceQuery = new ProvinceQuery();
        //将QueryBean转换成ProvinceQuery
        final String key = queryBean.getKey();
        String value = queryBean.getValue();
        if (StringUtils.hasText(key) && StringUtils.hasText(value)) {
            switch (key) {
                case "id":
                    provinceQuery.setId(Integer.valueOf(value));
                    break;
                case "name":
                    provinceQuery.setName(value);
                    break;
                case "area":
                    provinceQuery.setArea(value);
                    break;
                case "status":
                    provinceQuery.setStatus(Byte.valueOf(value));
                    break;
                default:
            }
        }
        PageBean<Province> pageBean = provinceService.getPage(provinceQuery, pageNum, pageSize);
        mav.setViewName("province_list");
        mav.addObject("pageBean", pageBean);
        return mav;
    }

    @ApiOperation(value = "查询指定编号的省份")
    @GetMapping("/getById")
    public ModelAndView getById(Integer provinceId, ModelAndView mav) {
        Province province = provinceService.getById(provinceId);
        mav.setViewName("province_details");
        mav.addObject("province", province);
        return mav;
    }

    @ApiOperation(value = "打开添加省份页面")
    @GetMapping("/openAddPage")
    public String openAddPage() {
        return "province_add";
    }

    @ApiOperation(value = "添加省份")
    @PostMapping("/add")
    public String add(@Valid Province province, BindingResult result) {
        if (result.hasErrors()) {
            //如果没有通过,跳转提示
            Map<String, String> map = getErrors(result);
            throw new WeGoArgumentException(map);
        } else {
            province.setStatus((byte) 1);
            provinceService.add(province);
            //重定向到列表页面
            return "redirect:/province/list";
        }
    }

    private Map<String, String> getErrors(BindingResult result) {
        Map<String, String> map = new HashMap<>();
        List<FieldError> list = result.getFieldErrors();
        for (FieldError error : list) {
            map.put(error.getField(), error.getDefaultMessage());
        }

        return map;
    }

    @ApiOperation(value = "打开修改省份页面")
    @GetMapping("/openUpdatePage")
    public ModelAndView openUpdatePage(Integer provinceId, ModelAndView mav) {
        Province province = provinceService.getById(provinceId);
        mav.setViewName("province_update");
        mav.addObject("province", province);
        return mav;
    }

    @ApiOperation(value = "修改省份")
    @PostMapping("/update")
    public String update(@Valid Province province, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, String> map = getErrors(result);
            throw new WeGoArgumentException(map);
        } else {
            provinceService.update(province);
            return "redirect:/province/list";
        }
    }

}